package com.mfy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mfy.entity.User;
import com.mfy.entity.vo.MsgVO;
import com.mfy.service.IUserService;
import com.mfy.utils.ImageUtils;
import com.mfy.utils.RedisPrefix;
import com.mfy.utils.RequiredToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author mfy
 * @since 2023-05-11
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @PostMapping("/tokens")
    public Map<String,String> login(@RequestBody MsgVO msgVO, HttpSession session){
//        1.到redis里面获取该手机号对应的验证码跟用户输入的验证码是否一致。
        String redisVerify=(String)redisTemplate.opsForValue().get(RedisPrefix.PHONE_KEY+msgVO.getPhone());
        if(StringUtils.equals(redisVerify,msgVO.getCaptcha())){
//        2.根据手机号到user表里面查询user对象，看数据库里面是否有该用户
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",msgVO.getPhone());
            User user = userService.getOne(wrapper);
//        3.没有就注册，有了就登录
            if(ObjectUtils.isEmpty(user)){
//        4.注册就是往数据库里面插入一个user对象。
                user =new User();
                user.setName(msgVO.getPhone());
                user.setAvatar(ImageUtils.getPhoto());
                user.setIntro("这个人很懒，什么都没有留下。。。");
                user.setPhone(msgVO.getPhone());
                user.setCreatedAt(LocalDateTime.now());
                user.setUpdatedAt(LocalDateTime.now());
                user.setFollowersCount(0);
                user.setFollowingCount(0);
                user.setPhoneLinked(true);
                user.setWechatLinked(false);
                userService.save(user);
            }

//        5.登录就是获取到sessionId，放入到redis里面key为sessionId，值为user对象。
            String token = session.getId();
            redisTemplate.opsForValue().set(RedisPrefix.USER_TOKEN_KEY+token,user,30, TimeUnit.MINUTES);
//        6.把sessionId放入到map里面返回
            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("token",token);
            return resultMap;
        }else{
            throw new RuntimeException("验证码不一致。。。");
        }
    }

    @GetMapping("/user")
    @RequiredToken
    public User userinfo(String token){
        User user = (User) redisTemplate.opsForValue().get(RedisPrefix.USER_TOKEN_KEY + token);
        if(ObjectUtils.isEmpty(user)){
            throw new RuntimeException("token异常。。。");
        }
        return user;
    }
}

